package com.dy.排序.内排序.交换排序.冒泡排序;

import java.util.Arrays;

public class BubbleSort {
    public static void bubbleSort(int[] nums) {
        for (int i = 0; i < nums.length; i++) {
            for (int j = nums.length - 1; j > i; j--) {
                if (nums[j] < nums[j - 1]) {
                    int tmp = nums[j];
                    nums[j] = nums[j - 1];
                    nums[j - 1] = tmp;
                }
            }
        }
    }

    public static void bubbleSort2(int[] nums) {
        boolean swapFlag = true;
        for (int i = 0; i < nums.length && swapFlag == true; i++) {//有交换时再进行下一趟
            swapFlag = false;
            for (int j = nums.length - 1; j > i; j--) {
                if (nums[j] < nums[j - 1]) {
                    swapFlag = true;
                    int tmp = nums[j];
                    nums[j] = nums[j - 1];
                    nums[j - 1] = tmp;
                }
            }
        }
    }

    public static void main(String[] args) {
        int[] nums = {3, 2, 4, 1, 5, 6};
        bubbleSort2(nums);
        Arrays.stream(nums).forEach(System.out::println);
    }
}
